package com.jinlia.show.modules.system.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinlia.show.modules.base.system.entity.SysUser;
import com.jinlia.show.modules.base.model.po.SysUserPO;
import com.jinlia.show.modules.base.model.query.UserPageQuery;
import com.jinlia.show.modules.system.model.vo.excel.UserExportVO;
import com.jinlia.show.modules.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class SysUserController {

    private final SysUserService userService;

    @PostMapping("/page")
    public ResponseEntity<IPage<SysUserPO>> getUserPage(@RequestBody UserPageQuery queryParams
    ) {
        IPage<SysUserPO> tt = userService.getUserPage(queryParams);
        return ResponseEntity.ok(tt);
    }

    @PostMapping("add")
    public ResponseEntity saveUser(@RequestBody SysUserPO sysUserVO) {
        return ResponseEntity.ok(userService.saveUser(sysUserVO));
    }



    //(summary = "修改用户")
    @PutMapping(value = "/update/{userId}")
    public ResponseEntity updateUser(@PathVariable Long userId,
                                     @RequestBody @Validated SysUserPO sysUserVO) {
        sysUserVO.setLastUpdater(null);
        sysUserVO.setLastUpdater(null);
        return ResponseEntity.ok(userService.updateUser(userId, sysUserVO));
    }

    //(summary = "删除用户")
    @DeleteMapping("/del/{id}")
    public ResponseEntity deleteUsers(@PathVariable String id) {
        return ResponseEntity.ok(userService.deleteUsers(id));
    }

    /**
     * 修改密码
     *
     * @param userId
     * @param password
     * @return
     */
    @GetMapping(value = "/{userId}/password")
    public ResponseEntity updatePassword(@PathVariable Long userId,
                                         @RequestParam String password) {
        return ResponseEntity.ok(userService.updatePassword(userId, password));
    }

    //(summary = "修改用户状态")
    @PatchMapping(value = "/{userId}/status")
    public ResponseEntity updateUserStatus(@PathVariable Long userId, @RequestParam Integer status
    ) {
        boolean update = userService.lambdaUpdate()
                .eq(SysUser::getId, userId)
                .set(SysUser::getEnableStatus, status)
                .update();
        return ResponseEntity.ok(update);
    }

    @GetMapping("me")
    public ResponseEntity<SysUserPO> getCurrentUserInfo() {
        SysUserPO userInfoVO = userService.getCurrentUserInfo();
        return ResponseEntity.ok(userInfoVO);
    }


    //(summary = "导入用户")
    @PostMapping("/_import")
    public ResponseEntity importUsers(Long deptId, MultipartFile file) throws IOException {
//        UserImportListener listener = new UserImportListener(deptId);
//        String msg = ExcelUtils.importExcel(file.getInputStream(), UserImportVO.class, listener);
        return ResponseEntity.ok(11);
    }

    //(summary = "导出用户")
    @GetMapping("/_export")
    public void exportUsers(UserPageQuery queryParams, HttpServletResponse response) throws IOException {
        String fileName = "用户列表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        List<UserExportVO> exportUserList = userService.listExportUsers(queryParams);
//        EasyExcel.write(response.getOutputStream(), UserExportVO.class).sheet("用户列表")
//                .doWrite(exportUserList);
    }

}
